package com.shashidi.weather.util;

import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;
import com.gtdev5.geetolsdk.mylibrary.util.SpUtils;
import com.gtdev5.geetolsdk.mylibrary.util.Utils;
import com.shashidi.weather.bean.CityNumberBean;

import java.lang.reflect.Type;
import java.util.ArrayList;
import java.util.List;

/**
 * 文件名：SpDefine
 * 创建者： lyq
 * 创建日期： 2020/11/4 14:53
 * 描述： 数据存储
 */

public

class SpDefine {

    // 当前位置信息
    public static final String NOW_CITY = "now_city";
    // 所有站点列表
    public static final String ALL_CITY = "all_city";

    /**
     * 获取当前位置信息
     */
    public static CityNumberBean getNowCityNumber() {
        if (Utils.isNotEmpty(SpUtils.getInstance().getString(NOW_CITY))) {
            Gson gson = new Gson();
            Type type = new TypeToken<CityNumberBean>() {}.getType();
            return gson.fromJson(SpUtils.getInstance().getString(NOW_CITY), type);
        }
        return null;
    }

    /**
     * 设置当前位置信息
     */
    public static void setNowCityNumber(CityNumberBean cityNumber) {
        Gson gson = new Gson();
        String value = gson.toJson(cityNumber);
        SpUtils.getInstance().putString(NOW_CITY, value);
    }

    /**
     * 获取所有站点列表
     */
    public static List<CityNumberBean> getAllCityNumbers() {
        if (Utils.isNotEmpty(SpUtils.getInstance().getString(ALL_CITY))) {
            Gson gson = new Gson();
            Type type = new TypeToken<List<CityNumberBean>>() {}.getType();
            List<CityNumberBean> items = gson.fromJson(SpUtils.getInstance().getString(ALL_CITY), type);
            return new ArrayList<>(items);
        }
        return null;
    }

    /**
     * 设置所有站点列表
     */
    public static void setAllCityNumber(List<CityNumberBean> list) {
        Gson gson = new Gson();
        String value = gson.toJson(list);
        SpUtils.getInstance().putString(ALL_CITY, value);
    }

}
